package com.utils.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 * 日期工具类
 */

public class DateUtil {

	private  final SimpleDateFormat format;

	public DateUtil(SimpleDateFormat format) {
		this.format = format;
	}

	public SimpleDateFormat getFormat() {
		return format;
	}
	// 紧凑型日期格式，也就是纯数字类型yyyyMMdd
	public static final DateUtil YEAR = new DateUtil(new SimpleDateFormat("yyyy"));

	// 紧凑型日期格式，也就是纯数字类型yyyyMMdd
	public static final DateUtil COMPAT = new DateUtil(new SimpleDateFormat("yyyyMMdd"));
	//紧凑型日期格式，也就是纯数字类型yyyyMMddHH
	public static final DateUtil COMPAT_HALF = new DateUtil(new SimpleDateFormat("yyyyMMddHH"));
	//完整日期格式，也就是纯数字类型yyyyMMddHHmmss
	public static final DateUtil COMPAT_FULL = new DateUtil(new SimpleDateFormat("yyyyMMddHHmmss"));

	// 常用日期格式，yyyy-MM-dd
	public static final DateUtil COMMON = new DateUtil(new SimpleDateFormat("yyyy-MM-dd"));

	public static final DateUtil COMMON_FULL = new DateUtil(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
	public static final DateUtil COMMON_HALF = new DateUtil(new SimpleDateFormat("yyyy-MM-dd HH:mm"));

	// 使用斜线分隔的，西方多采用，yyyy/MM/dd
	public static final DateUtil SLASH = new DateUtil(new SimpleDateFormat("yyyy/MM/dd"));
	// 使用斜线分隔的，西方多采用，dd/MM/yyyy
	public static final DateUtil RLASH = new DateUtil(new SimpleDateFormat("dd/MM/yyyy"));

	// 中文日期格式常用，yyyy年MM月dd日
	public static final DateUtil CHINESE = new DateUtil(new SimpleDateFormat("yyyy年MM月dd日"));

	public static final DateUtil CHINESE_FULL = new DateUtil(new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"));
	
	// 简单日期格式，yyyy-MM
	public static final DateUtil SIMPLE = new DateUtil(new SimpleDateFormat("yyyy-MM"));
    
    private static Calendar calendar = new GregorianCalendar();
    
   
	/**
	 * 秒时间戳格式化
	 */
    public  String getStrTime(int time) {  
    	String re_StrTime = null;      	      	  
    	// 例如：cc_time=1291778220  
    	long lcc_time = Long.valueOf(time);  
    	re_StrTime = getFormat().format(new Date(lcc_time * 1000L));      	      	  
    	return re_StrTime;      	     	  
    }  
    
   

	/**
	 * 日期获取字符串
	 */
	public String getDateText(Date date) {
		return getFormat().format(date);
	}
	

	/**
	 * 字符串获取日期
	 * 
	 * @throws ParseException
	 */
	public  Date getTextDate(String text) throws ParseException {
		return getFormat().parse(text);
	}

	/**
	 * 日期获取字符串
	 */
	public static String getDateText(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}
	

	/**
	 * 字符串获取日期
	 * 
	 * @throws ParseException
	 */
	public static Date getTextDate(String dateText, String format) throws ParseException {
		return new SimpleDateFormat(format).parse(dateText);
	}

	/**
	 * 根据日期，返回其星期数，周一为1，周日为7
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeekDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int w = calendar.get(Calendar.DAY_OF_WEEK);
		int ret;
		if (w == Calendar.SUNDAY)
			ret = 7;
		else
			ret = w - 1;
		return ret;
	}

	
	/**
	 * 根据出生日期获取某人岁数
	 * 
	 * @param date
	 * @return
	 * @throws ParseException 
	 */
	public static int getAge(String birthday,String format) throws ParseException {
		Date date=new SimpleDateFormat(format).parse(birthday);
		Calendar calendar = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(date);
		if (calendar.before(calendar2)) {
			throw new IllegalArgumentException("出生时间大于当前时间!");
		}
		int yearNow = calendar.get(Calendar.YEAR);
		int monthNow = calendar.get(Calendar.MONTH) + 1;// 注意此处，如果不加1的话计算结果是错误的
		int dayOfMonthNow = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.setTime(date);
		int yearBirth = calendar.get(Calendar.YEAR);
		int monthBirth = calendar.get(Calendar.MONTH);
		int dayOfMonthBirth = calendar.get(Calendar.DAY_OF_MONTH);
		int age = yearNow - yearBirth;
		if (monthNow < monthBirth || (monthNow == monthBirth && dayOfMonthNow < dayOfMonthBirth)) {
			age--;
		}
		return age;

	}
	
	

	
	/**
	 * 获取某月总天数
	 * @return
	 */
	public static int getMonthday(String source){
  	    try {
  	      DateFormat df=new SimpleDateFormat("yyyy-MM");
  	      Date date = df.parse(source);
  	      calendar.setTime(date);   
  	    } catch (Exception e) {
  	      e.printStackTrace();
  	    }
  	   return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * 产生随机的三位数
	 * @return
	 */
	public static String getThree(){
		Random rad=new Random();
		return rad.nextInt(1000)+"";
	}
	
}
